function redering() {
  $.ajax({
    url:'/admin/links',
    success:function (res) {
      let add = template('tmp-table',res)
      $('tbody').html(add)
    }
  })
}
redering()
$('button:contains("添加链接")').on('click',function () {
  let index = layer.open({
    type:1,
    title: '添加友情链接',
    content: $('#add-form').html(),
    area: ['500px','350px'],
  })
  $('#test1').on('click',function () {
    $('#linkFile').trigger('click')
  })
  $('#linkFile').on('change',function (e) {
    let imgurl = URL.createObjectURL(e.target.files[0])
    $('#preIcon').attr('src',imgurl)
  })
  $('#tmp-form').on('submit',function (e) {
    e.preventDefault()
    let fd =new FormData(this)
    $.ajax({
      type:'post',
      url:'/admin/links',
      data: fd,
      processData: false,
      contentType: false,
      success:function (res) {
        layer.msg(res.message)
        if(res.status === 0){
          layer.close(index)
          redering()
        }
      }
    })
  })
})

$('.layui-table tbody').on('click','button:contains("删除")',function(e){
  let id = $(e.target).data('id')
  layer.confirm('确定要删除吗',function (index) {
    
    $.ajax({
      url:'/admin/links/'+id,
      type:'DELETE',
      success:function(res){
        layer.msg(res.message)
        if(res.status===0){
          layer.close(index)
          redering()
        }
      }
    })
  })
})

let form = layui.form

$('.layui-table tbody').on('click','button:contains("编辑")',function () {
  let id =$(this).data('id')
  $.ajax({
    type:'get',
    url:'/admin/links/'+id,
    success:function (res) {
      let label = layer.open({
        type:1,
        title: '修改友情链接',
        content: $('#editor-form').html(),
        area: ['500px','350px'],
      })
      console.log(res)
      $('#preIcon').attr('src', 'http://localhost:8888/uploads/' + res.data.linkicon)
      delete res.data.linkicon
      form.val('editor',res.data)




      $('#test1').on('click',function () {
        $('#linkFile').trigger('click')
      })
      $('#linkFile').on('change',function (e) {
        let imgurl = URL.createObjectURL(e.target.files[0])
        $('#preIcon').attr('src',imgurl)
      })
      $('body').on('submit','#editor-form',function (e) {
        console.log(111)
        e.preventDefault()
        let fd =new FormData(this)
        $.ajax({
          type:'PUT',
          url:'/admin/links/'+id,
          data: fd,
          processData: false,
          contentType: false,
          success:function (res) {
            layer.msg(res.message)
            if(res.status === 0){
              layer.close(label)
              redering()
            }
          }
        })
      })
    }
  })
  
})


